import {defineStore} from "pinia"
import mockRequest from "@/mock/request"
import initRoleListTable from "./initRoleTable"
import {authBtnMode} from "@/common/config"

export const actionKeys={
  queryList:authBtnMode=='request'?'roleListQuery':'query',
  add:authBtnMode=='request'?'roleAdd':'add',
  update:authBtnMode=='request'?'roleUpdate':'update',
  delete:authBtnMode=='request'?'roleDelete':'delete',
}

export type actionKeyType='add' | 'delete' | 'update' | 'query' | 'import' | 'export' | 'download'
export type actionConfigType={label:string,value:actionKeyType}
export const actionConfig:actionConfigType[]=[
	{ label: '新增', value: 'add' },
	{ label: '删除', value: 'delete' },
	{ label: '更新', value: 'update' },
	{ label: '查询', value: 'query' },
	{ label: '导入', value: 'import' },
	{ label: '导出', value: 'export' },
	{ label: '下载', value: 'download' },
]
export const actionConfigMap:Record<actionKeyType,actionConfigType>={
  add:{ label: '新增', value: 'add' },
	delete:{ label: '删除', value: 'delete' },
	update:{ label: '更新', value: 'update' },
	query:{ label: '查询', value: 'query' },
	import:{ label: '导入', value: 'import' },
	export:{ label: '导出', value: 'export' },
	download:{ label: '下载', value: 'download' },
}

export default defineStore('roleList',{
    state:()=>({
      list:[] as RoleItem[]
    }),
    actions:{
    /** 
      @requestName roleListQuery  接口权限值
    **/
      async getList(){
        const res = await mockRequest(()=>({
          code: 200,
          list: this.list.length ? this.list : initRoleListTable,
        }))
        if (res && res.code) {
          this.list = res.list
          return { code: 200, list: res.list }
        }
        return { code: 0 }
      },
      /** 
      @requestName roleDelete  接口权限值
    **/
      async deleteItem(id:string|number){
         const res=await mockRequest(()=>{
          this.list.splice(this.list.findIndex(v=>v.id==id),1)
          return ({code:200}) 
         },false)
         if(res&&res.code) return{code:200}
         return{code:0}
      },
      /** 
      @requestName roleAdd  接口权限值
    **/
      async addItem(role:RoleItem){
         const res=await mockRequest(()=>{
          if(!this.list.find(v=>role.roleName==v.roleName))
              {
                this.list.unshift(role)
                return ({code:200})
              }
              else  return ({code:201})
         })
         if (res && res.code == 200) {return { code: 200 }}
         if (res && res.code == 201) return { code: 201,msg:'已存在'}
         return { code: 0 }
      },
    /** 
      @requestName roleUpdate  接口权限值
    **/
      async updateItem(role:RoleItem){
         const res=await mockRequest(()=>{
          if(!this.list.find(v=>v.id!=role.id && role.roleName==v.roleName))
          {
            const item=this.list.find(v=>v.id==role.id)
            Object.keys(role).forEach((k)=>{
            item![k]=role[k]
          })
            return({code:200})
          }
          else return({code:201})
         })
         if (res && res.code == 200) {return { code: 200 }}
         if (res && res.code == 201) return { code: 201,msg:'已存在'}
         return { code: 0 }
      },
    },
    

})